/**
 * File    : PropertyManagerImpl.java
 * Created : 2008-10-9
 * By      : LuHuiguo
 */
package com.luhuiguo.sso.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

import com.luhuiguo.sso.dao.PropertyDao;
import com.luhuiguo.sso.entity.Property;
import com.luhuiguo.sso.exception.NoSuchPropertyException;
import com.luhuiguo.sso.service.PropertyManager;

/**
 * @author LuHuiguo
 * @version $Id: PropertyManagerImpl.java 19 2008-10-18 15:59:28Z LuHuiguo $
 * 
 */
@Transactional
public class PropertyManagerImpl implements PropertyManager {

	private PropertyDao propertyDao;

	@Required
	public void setPropertyDao(PropertyDao propertyDao) {
		this.propertyDao = propertyDao;
	}

	@Override
	public void createProperty(Property property) {
		propertyDao.create(property);
	}

	@Override
	public void deleteProperty(Long id) {
		propertyDao.delete(id);

	}

	@Override
	public void deleteProperty(String name) {
		deleteProperty(getProperty(name));
	}

	@Override
	public void deleteProperty(Property property) {
		propertyDao.delete(property);

	}

	@Transactional(readOnly = true)
	@Override
	public List<Property> getAllProperty(int... rowStartIdxAndCount) {
		return propertyDao.findAll(rowStartIdxAndCount);
	}

	@Transactional(readOnly = true)
	@Override
	public Property getProperty(Long id) {
		return propertyDao.get(id);
	}

	@Override
	@Transactional(readOnly = true)
	public Property getProperty(String name) {
		List<Property> properties = propertyDao.findByProperty("name", name); //$NON-NLS-1$
		if (properties.size() == 0) {
			throw new NoSuchPropertyException("no such property:" + name);
		}
		return properties.get(0);
	}

	@Transactional(readOnly = true)
	@Override
	public boolean existsProperty(String name) {
		return getProperty(name) != null;
	}

	@Override
	public void updateProperty(Property property) {
		propertyDao.update(property);
	}

	@Override
	public void setProperty(String name, String value) {
		Property p = null;
		try {
			p = getProperty(name);
		} catch (NoSuchPropertyException e) {

		}
		if (p == null) {
			p = new Property(name, value);
			createProperty(p);
		} else {
			p.setValue(value);
			updateProperty(p);
		}

	}

	@Override
	public void setProperty(String name, boolean value) {
		setProperty(name, Boolean.toString(value));
	}

	@Override
	public void setProperty(String name, int value) {
		setProperty(name, Integer.toString(value));
	}

	@Override
	public void setProperty(String name, double value) {
		setProperty(name, Double.toString(value));
	}

	@Transactional(readOnly = true)
	@Override
	public boolean getBoolean(String name) {
		Property p = getProperty(name);
		return Boolean.parseBoolean(p.getValue());
	}

	@Transactional(readOnly = true)
	@Override
	public boolean getBoolean(String name, boolean defaultValue) {
		try {
			Property p = getProperty(name);
			return Boolean.parseBoolean(p.getValue());
		} catch (Exception e) {
			return defaultValue;
		}
	}

	@Transactional(readOnly = true)
	@Override
	public double getDouble(String name) {
		Property p = getProperty(name);
		return Double.parseDouble(p.getValue());
	}

	@Transactional(readOnly = true)
	@Override
	public double getDouble(String name, double defaultValue) {
		try {
			Property p = getProperty(name);
			return Double.parseDouble(p.getValue());
		} catch (Exception e) {
			return defaultValue;
		}

	}

	@Transactional(readOnly = true)
	@Override
	public int getInt(String name) {
		Property p = getProperty(name);
		return Integer.parseInt(p.getValue());
	}

	@Transactional(readOnly = true)
	@Override
	public int getInt(String name, int defaultValue) {
		try {
			Property p = getProperty(name);
			return Integer.parseInt(p.getValue());
		} catch (Exception e) {
			return defaultValue;
		}
	}

	@Transactional(readOnly = true)
	@Override
	public String getString(String name) {
		return getString(name, null);
	}

	@Transactional(readOnly = true)
	@Override
	public String getString(String name, String defaultValue) {
		try {
			Property p = getProperty(name);
			return p.getValue();

		} catch (Exception e) {
			return defaultValue;
		}

	}

	@Override
	public boolean isEnableCheckImmediateMode() {
		return getBoolean("enableCheckImmediateMode", false);
	}

	@Override
	public void setEnableCheckImmediateMode(boolean enableCheckImmediateMode) {
		setProperty("enableCheckImmediateMode", enableCheckImmediateMode);
	}

	@Override
	public boolean isEnableStatelessMode() {
		return getBoolean("enableStatelessMode", false);
	}

	@Override
	public void setEnableStatelessMode(boolean enableStatelessMode) {
		setProperty("enableStatelessMode", enableStatelessMode);

	}

	@Override
	public boolean isEnableRelyingPartyLocalhostMode() {
		return getBoolean("enableRelyingPartyLocalhostMode", false);
	}

	@Override
	public void setEnableRelyingPartyLocalhostMode(
			boolean enableRelyingPartyLocalhostMode) {
		setProperty("enableRelyingPartyLocalhostMode",
				enableRelyingPartyLocalhostMode);

	}

	@Override
	public boolean isEnableCaptchaOnLogin() {
		return getBoolean("enableCaptchaOnLogin", false);

	}

	@Override
	public void setEnableCaptchaOnLogin(boolean enableCaptchaOnLogin) {
		setProperty("enableCaptchaOnLogin", enableCaptchaOnLogin);

	}

	@Override
	public boolean isEnableCaptchaOnRegistration() {
		return getBoolean("enableCaptchaOnRegistration", false);

	}

	@Override
	public void setEnableCaptchaOnRegistration(
			boolean enableCaptchaOnRegistration) {
		setProperty("enableCaptchaOnRegistration", enableCaptchaOnRegistration);

	}

	@Override
	public boolean isEnableEmailConfirmationOnRegistration() {
		return getBoolean("enableEmailConfirmationOnRegistration", false);

	}

	@Override
	public void setEnableEmailConfirmationOnRegistration(
			boolean enableEmailConfirmationOnRegistration) {
		setProperty("enableEmailConfirmationOnRegistration",
				enableEmailConfirmationOnRegistration);

	}

	@Override
	public String getBaseURL() {
		return getString("baseURL", "");
	}

	@Override
	public void setBaseURL(String baseURL) {
		setProperty("baseURL", baseURL);

	}

}
